<?php

$file = file('extract2confirmed.csv');
$top  = array();

$modules = array();

foreach ($file as $line) {
    if (strlen(trim($line))==0) {
        if (is_array($tmp) && !empty($tmp)) {
            $top[] = $tmp;
        }
        $tmp = array(); // reset
    } else {
        $tmp[] = $line;
        
        $xpl = explode("\t", $line);
        if (isset($xpl[0]) && strlen($xpl[0])>2) {
            $modules[$xpl[0]] = true;
        }
        
    }
}

$modules = json_decode("[".join(',', array_keys($modules))."]", true);

$sql = array();

foreach ($modules as $one) {
    $sql[] = "SELECT '{$one}' AS name, (SELECT id FROM module WHERE LCASE(title)='".strtolower($one)."') AS id";
}

trap(join("\nUNION\n", $sql));


















/**
 * Creates output of the subject passed, by default halting further execution
 * 
 * @param mixed $subject Element that you need insight into
 * 
 * @param bool  $halt    Flag (optional) if TRUE execution halts after dump
 * 
 * @return void
 */
function trap($subject, $halt=true)
{
    $cli = (isset($_SERVER['TERM'])) ? true : false;
    
    try {
        throw new Exception('DUMMIE');
    } catch (Exception $xc) {
    }
    
    ob_start();
    var_dump($subject);
    $dump = ob_get_clean();
    
    $details = array(
        'Details'     => print_r($subject, true),
        'Dump'        => $dump,
        'Stack trace' => print_r($xc->getTrace(), true)
    );
    
    echo ($cli!==true) ? "<pre>" : chr(27)."[H".chr(27)."[2J"."\n";
    
    foreach ($details as $descr => $data) {
        
        if (true===$cli) {
            echo "\n\n------------- {$descr} -----------------\n\n{$data}\n";
        } else {
            echo "<h3>{$descr}</h3><br>{$data}<br>";
        }
    }
    
    echo ($cli!==true) ? "<pre>" : "\n";
    
    if ($halt===true) {
        exit();
    }
}